//
//  PickerPeriodView.swift
//  SmallParts
//
//  Created by 航汇聚 on 2019/11/26.
//  Copyright © 2019 BellaZhou. All rights reserved.
//

import UIKit
typealias passYearAndPeriod = (_ year: String, _ indeOfYear: Int, _ period: String, _ indexOfPeriod: Int) -> ()
class PickerPeriodView: UIView, NibLoadable {
    @IBOutlet weak var pickerView: UIPickerView!
    @IBAction func tapVisualEffectView(_ sender: UITapGestureRecognizer) {
        self.removeFromSuperview()
    }
    @IBAction func touchCancelAction(_ sender: UIButton) {
        self.removeFromSuperview()
    }
    @IBAction func touchConfirmAction(_ sender: UIButton) {
        let numObj = self.periodicalNumArray![self.indexOfSelectYear!]
        let rowArray = numObj.num.components(separatedBy: ",")
        if self.indexOfSelectYear! < self.periodicalNumArray!.count && self.indexOfSelectPeriod! < rowArray.count {
            let year = self.years![self.indexOfSelectYear!]
            let period = rowArray[self.indexOfSelectPeriod!]
            if self.passYearAndPeriodBlock != nil {
                self.passYearAndPeriodBlock!(year, self.indexOfSelectYear!, period, self.indexOfSelectPeriod!)
            }
            print("第\(year)年第\(period)期")
            self.removeFromSuperview()
        }
    }
    var years: [String]?
    var indexOfSelectYear: Int?
    var indexOfSelectPeriod: Int?
    var periodicalNumArray: [MediaNumModel]? {
        didSet {
            self.reCreateData(periodicalNumArray!)
        }
    }
    var passYearAndPeriodBlock: passYearAndPeriod?
    func reCreateData(_ periodicalNumberArray: [MediaNumModel]) {
        let tempYears = NSMutableArray.init()
        for numItem in periodicalNumberArray {
            tempYears.add(numItem.year)
        }
        self.years = (tempYears as! [String])
        self.pickerView.reloadAllComponents()
        self.pickerView.selectRow(self.indexOfSelectYear!, inComponent: 0, animated: true)
        self.pickerView.selectRow(self.indexOfSelectPeriod!, inComponent: 1, animated: true)
    }
}
extension PickerPeriodView: UIPickerViewDataSource, UIPickerViewDelegate {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0:
            return self.years == nil ? 0 : self.years!.count
        default:
            if self.periodicalNumArray == nil {
                return 0
            }
            let numObj = self.periodicalNumArray![self.indexOfSelectYear!]
            return numObj.num.components(separatedBy: ",").count
        }
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch component {
        case 0:
            return "\(self.years![row])年"
        default:
            let numObj = self.periodicalNumArray![self.indexOfSelectYear!]
            let rowArray = numObj.num.components(separatedBy: ",")
            return "第\(rowArray[row])期"
        }
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0:
            self.indexOfSelectYear = row
            break
        default:
            self.indexOfSelectPeriod = row
            break
        }
        pickerView.reloadAllComponents()
    }
}
